Page({
  isSuccessCall: false,
  timer: '',
  data: {
    second: 30,
    clearSeconds: false,
  },
  onLoad (options) {
    console.log('options', options)
    this.talk(options);
    this.timer = setInterval(() => {
      if (this.data.second <= 0) {
        clearTimeout(this.timer);
        if (this.isSuccessCall) {
          this.setData({
            clearSeconds: true
          })
          return
        } else {
          wx.showToast({
            title: '设计师正忙.'
          });
          wx.navigateBack();         
        }
      }
      this.setData({
        second: this.data.second - 1
      })
    }, 1000);
    wx.eventBus.on('_phone_end', (sessionId) => {
      console.log('结束通话sessionId',sessionId)
      wx.showToast({
        title: '通话结束'
      })
      wx.navigateBack()
    })
  },
  talk (options) {
    var me = this;
    // 调用通讯接口TODO
    wx.request({
      url: wx.envConfig.host + '/call/call?params=' + JSON.stringify({order_id: options.orderId ,recUid: options.recUid,callUid: options.callUid}),
      method: 'POST',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res.data)
        if (res.data.error_code != 'Y10000') {
          wx.showToast({
            title: '通话失败'
          });
          wx.navigateBack();
        } else {
          // clearTimeout(me.timer);
          me.isSuccessCall = true
        }
      }
    })
  }
})